package com.bmob.lover.ui.fragment.festival

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.getValueNonNull
import com.bmob.lover.R
import com.bmob.lover.app.ext.toJson
import com.bmob.lover.data.model.bean.Background
import com.bmob.lover.data.model.bean.TheFile
import com.bmob.lover.ui.fragment.base.BaseImageFragment
import com.bmob.lover.viewmodel.FestivalBgListViewModel
import com.bmob.lover.viewmodel.request.FestivalBgSaveViewModel
import com.theone.common.callback.IImageUrl
import com.theone.mvvm.ext.qmui.addLeftCloseImageBtn
import com.theone.mvvm.ext.qmui.showFailTipsDialog
import com.theone.mvvm.ext.qmui.showSuccessTipsDialog

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-18 09:41
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class FestivalBgListFragment private constructor() :
    BaseImageFragment<TheFile, FestivalBgListViewModel>() {

    companion object {
        fun newInstance(bean: Background): FestivalBgListFragment {
            return FestivalBgListFragment().apply {
                arguments = Bundle().apply {
                    putParcelable(BundleConstant.DATA, bean)
                }
            }
        }
    }

    private val mPublishVm: FestivalBgSaveViewModel by viewModels()
    private val mBackground: Background by getValueNonNull(BundleConstant.DATA)

    private var mSelectFile: TheFile? = null
    private var mSelectPosition: Int = 0

    override fun initView(root: View) {
        super.initView(root)
        getTopBar()?.run {
            addLeftCloseImageBtn(R.drawable.mz_comment_titlebar_ic_close_light)
            setTitle(mBackground.key)
            addRightImageButton(
                R.drawable.icon_add_light,
                R.id.topbar_right_view
            ).setOnClickListener {
                startFragmentAndDestroyCurrent(FestivalBgAddFragment.newInstance(mBackground))
            }
        }
    }

    override fun initData() {
        getViewModel().datas = mBackground.getFiles()
    }

    override fun createObserver() {
        super.createObserver()
        mPublishVm.getObjectSuccessLiveDate().observe(this, Observer {
            showSuccessTipsDialog("更新成功")
            getAdapter().data.remove(mSelectFile!!)
            getAdapter().notifyItemRemoved(mSelectPosition)
            getAdapter().notifyItemRangeChanged(mSelectPosition, getAdapter().data.size)
        })
        mPublishVm.getErrorLiveData().observe(this, Observer {
            showFailTipsDialog(it)
        })
    }

    override fun onLoadMoreComplete() {
        super.onLoadMoreComplete()
        getRefreshLayout()?.isEnabled = false
        getAdapter().loadMoreModule.loadMoreEnd(true)
    }

    override fun onScrollChanged(item: TheFile, position: Int, total: Int) {
        getTopBar()?.setSubTitle("${position + 1}/$total")
    }

    override fun onPreviewLongClick(media: IImageUrl, position: Int) {
        mSelectFile = media as TheFile
        mSelectPosition = position
        showBottomActionDialog()
    }

    private fun showBottomActionDialog() {
        QMUIBottomSheet.BottomListSheetBuilder(mActivity).apply {
            setAddCancelBtn(true)
            setGravityCenter(true)
            addItem("删除")
            setOnSheetItemClickListener { dialog, itemView, position, tag ->
                dialog.dismiss()
                deleteFile()
            }
        }.build().show()
    }

    private fun deleteFile() {
        val list = mutableListOf<TheFile>()
        for (file in mBackground.getFiles()) {
            if (file.path != mSelectFile?.path) {
                list.add(file)
                break
            }
        }
        mBackground.attachments = list.toJson()
        mPublishVm.update(mBackground)
    }

}